package cn.youqu.happy.module.youqu.controller.admin.userFollows;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.*;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.youqu.happy.framework.common.pojo.PageResult;
import cn.youqu.happy.framework.common.pojo.CommonResult;
import static cn.youqu.happy.framework.common.pojo.CommonResult.success;

import cn.youqu.happy.framework.excel.core.util.ExcelUtils;

import cn.youqu.happy.framework.operatelog.core.annotations.OperateLog;
import static cn.youqu.happy.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.youqu.happy.module.youqu.controller.admin.userFollows.vo.*;
import cn.youqu.happy.module.youqu.dal.dataobject.userFollows.UserFollowsDO;
import cn.youqu.happy.module.youqu.convert.userFollows.UserFollowsConvert;
import cn.youqu.happy.module.youqu.service.userFollows.UserFollowsService;

@Api(tags = "管理后台 - 用户关注")
@RestController
@RequestMapping("/youqu/user-follows")
@Validated
public class UserFollowsController {

    @Resource
    private UserFollowsService userFollowsService;

    @PostMapping("/create")
    @ApiOperation("创建用户关注")
    @PreAuthorize("@ss.hasPermission('youqu:user-follows:create')")
    public CommonResult<Integer> createUserFollows(@Valid @RequestBody UserFollowsCreateReqVO createReqVO) {
        return success(userFollowsService.createUserFollows(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新用户关注")
    @PreAuthorize("@ss.hasPermission('youqu:user-follows:update')")
    public CommonResult<Boolean> updateUserFollows(@Valid @RequestBody UserFollowsUpdateReqVO updateReqVO) {
        userFollowsService.updateUserFollows(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除用户关注")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Integer.class)
    @PreAuthorize("@ss.hasPermission('youqu:user-follows:delete')")
    public CommonResult<Boolean> deleteUserFollows(@RequestParam("id") Integer id) {
        userFollowsService.deleteUserFollows(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得用户关注")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Integer.class)
    @PreAuthorize("@ss.hasPermission('youqu:user-follows:query')")
    public CommonResult<UserFollowsRespVO> getUserFollows(@RequestParam("id") Integer id) {
        UserFollowsDO userFollows = userFollowsService.getUserFollows(id);
        return success(UserFollowsConvert.INSTANCE.convert(userFollows));
    }

    @GetMapping("/list")
    @ApiOperation("获得用户关注列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('youqu:user-follows:query')")
    public CommonResult<List<UserFollowsRespVO>> getUserFollowsList(@RequestParam("ids") Collection<Integer> ids) {
        List<UserFollowsDO> list = userFollowsService.getUserFollowsList(ids);
        return success(UserFollowsConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得用户关注分页")
    @PreAuthorize("@ss.hasPermission('youqu:user-follows:query')")
    public CommonResult<PageResult<UserFollowsRespVO>> getUserFollowsPage(@Valid UserFollowsPageReqVO pageVO) {
        PageResult<UserFollowsDO> pageResult = userFollowsService.getUserFollowsPage(pageVO);
        return success(UserFollowsConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出用户关注 Excel")
    @PreAuthorize("@ss.hasPermission('youqu:user-follows:export')")
    @OperateLog(type = EXPORT)
    public void exportUserFollowsExcel(@Valid UserFollowsExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<UserFollowsDO> list = userFollowsService.getUserFollowsList(exportReqVO);
        // 导出 Excel
        List<UserFollowsExcelVO> datas = UserFollowsConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "用户关注.xls", "数据", UserFollowsExcelVO.class, datas);
    }

}
